package com.ihr360.cnb.module.insurance.service.impl;

import com.ihr360.authority.embed.base.entity.AuthStaffDataRequest;
import com.ihr360.cnb.module.common.service.Ihr360StaffApiService;
import com.ihr360.cnb.module.common.service.OrganizationService;
import com.ihr360.cnb.module.insurance.model.StaffDataAuthorityCodeConstants;
import com.ihr360.cnb.module.insurance.service.MemberPlanAggregateService;
import com.ihr360.cnb.module.insurance.service.feign.InsuranceMemberPlanService;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.vo.PageData;
import com.ihr360.excel.ExcelUtil;
import com.ihr360.excel.entity.ExportParams;
import com.ihr360.insurance.entity.request.MemberPlanReq;
import com.ihr360.insurance.entity.request.StaffIdRequest;
import com.ihr360.insurance.entity.vo.MemberPlanVo;
import com.ihr360.validation.ValidationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author Stone.Shi
 * @description
 * @date 2018-05-24 18:03:35.
 */
@Service
public class MemberPlanAggregateServiceImpl implements MemberPlanAggregateService {

    private static final Logger logger = LoggerFactory.getLogger(MemberPlanAggregateServiceImpl.class);

    @Autowired
    private Ihr360StaffApiService staffApiService;

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private InsuranceMemberPlanService insuranceMemberPlanService;

    @Override
    public PageData<MemberPlanVo> getIncreaseMember(String companyId, String userId, MemberPlanReq req) {
        AuthStaffDataRequest staffDataRequest = getStaffDataRequest(req, StaffDataAuthorityCodeConstants.ARCHIVE_AUTHORITY_CODE_VIEW);
        List<String> authStaffIds = staffApiService.getAuthStaffIds(companyId, userId, staffDataRequest);
        if (CollectionUtils.isEmpty(authStaffIds)) {
            return new PageData<>(0, 0, Collections.emptyList());
        }
        req.setStaffIds(authStaffIds);
        return insuranceMemberPlanService.getIncreaseMemberList(companyId, userId, req);
    }

    @Override
    public void saveIncreaseMemberRecords(String companyId, String userId, Long ledgerSummaryId) {
        AuthStaffDataRequest staffDataRequest = getStaffDataRequest(null, StaffDataAuthorityCodeConstants.ARCHIVE_AUTHORITY_CODE_VIEW);
        List<String> authStaffIds = staffApiService.getAuthStaffIds(companyId, userId, staffDataRequest);
        if (CollectionUtils.isEmpty(authStaffIds)) {
            return;
        }
        insuranceMemberPlanService.saveIncreaseMembers(ledgerSummaryId, new StaffIdRequest(authStaffIds));
    }

    @Override
    public void exportExcelIncreaseMember(String companyId, String userId,
                                          MemberPlanReq req, ServletOutputStream outputStream) {
        AuthStaffDataRequest staffDataRequest = getStaffDataRequest(req, StaffDataAuthorityCodeConstants.ARCHIVE_AUTHORITY_CODE_VIEW);
        List<String> authStaffIds = staffApiService.getAuthStaffIds(companyId, userId, staffDataRequest);
        if (CollectionUtils.isEmpty(authStaffIds)) {
            return;
        }
        req.setStaffIds(authStaffIds);
        Map<Long, String> departmentFullNames = organizationService.getDepartmentFullNames(companyId);
        req.setDepartmentFullNames(departmentFullNames);
        ExportParams<List<Object>> exportExcelData = insuranceMemberPlanService.getExportExcelDataIncrease(companyId, userId, req);

        try {
            ExcelUtil.exportExcel(exportExcelData, outputStream);
            outputStream.close();
        } catch (IOException e) {
            ValidationUtils.reject("", "增员表数据导出失败,请刷新重试");
            logger.info(e.getMessage());
        }
    }

    @Override
    public PageData<MemberPlanVo> getDecreaseMember(String companyId, String userId, MemberPlanReq req) {
        AuthStaffDataRequest staffDataRequest = getStaffDataRequest(req, StaffDataAuthorityCodeConstants.ARCHIVE_AUTHORITY_CODE_VIEW);
        List<String> authStaffIds = staffApiService.getAuthStaffIds(companyId, userId, staffDataRequest);
        if (CollectionUtils.isEmpty(authStaffIds)) {
            return new PageData<>(0, 0, Collections.emptyList());
        }
        req.setStaffIds(authStaffIds);
        return insuranceMemberPlanService.getDecreaseMemberList(companyId, userId, req);
    }

    @Override
    public void saveDecreaseMemberRecords(String companyId, String userId, Long ledgerSummaryId) {
        AuthStaffDataRequest staffDataRequest = getStaffDataRequest(null, StaffDataAuthorityCodeConstants.ARCHIVE_AUTHORITY_CODE_VIEW);
        List<String> authStaffIds = staffApiService.getAuthStaffIds(companyId, userId, staffDataRequest);
        if (CollectionUtils.isEmpty(authStaffIds)) {
            return;
        }
        insuranceMemberPlanService.saveDecreaseMembers(ledgerSummaryId, new StaffIdRequest(authStaffIds));
    }

    @Override
    public void exportExcelDecreaseMember(String companyId, String userId, MemberPlanReq req, ServletOutputStream outputStream) {
        AuthStaffDataRequest staffDataRequest = getStaffDataRequest(req, StaffDataAuthorityCodeConstants.ARCHIVE_AUTHORITY_CODE_VIEW);
        List<String> authStaffIds = staffApiService.getAuthStaffIds(companyId, userId, staffDataRequest);
        if (CollectionUtils.isEmpty(authStaffIds)) {
            return;
        }
        req.setStaffIds(authStaffIds);
        Map<Long, String> departmentFullNames = organizationService.getDepartmentFullNames(companyId);
        req.setDepartmentFullNames(departmentFullNames);
        ExportParams<List<Object>> exportExcelData = insuranceMemberPlanService.getExportExcelDataDecrease(companyId, userId, req);

        try {
            ExcelUtil.exportExcel(exportExcelData, outputStream);
            outputStream.close();
        } catch (IOException e) {
            ValidationUtils.reject("", "减员表数据导出失败,请刷新重试");
            logger.info(e.getMessage());
        }
    }

    private AuthStaffDataRequest getStaffDataRequest(MemberPlanReq req, String authorityCodeView) {
        AuthStaffDataRequest staffDataRequest = new AuthStaffDataRequest();
        staffDataRequest.setAuthorityCode(authorityCodeView);
        if (req != null) {
            staffDataRequest.setStaffName(req.getStaffName());
            staffDataRequest.setIdCardNo(req.getIdCardNo());
            staffDataRequest.setDepartIdList(req.getDepartmentIds());
        }
        return staffDataRequest;
    }
}
